#include <bits/stdc++.h>
using namespace std;
inline bool isprime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (!(n % i)) {
            return false;
        }
    }
    return true;
}
inline bool isspec(int n) {
    if (n % 20 == 3 || n <= 10) {
        return isprime(n);
    } else {
        return false;
    }
}
inline int med(int a, int b, int c) {
    int arr[3] = {a, b, c};
    sort(arr, arr+3);
    return arr[1];
}
bool temp[60010];
int T, n, m, x[8010], y[8010], o[8010];
inline int dis(int i, int j) {
    return abs(x[i] - x[j]) + abs(y[i] - y[j]);
}
long long ans;
int main() {
    for (int i = 0; i <= 60000; i++) {
        temp[i] = isspec(i);
    }
    freopen("chess.in", "r", stdin);
    freopen("chess.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> T;
    while (T--) {
        cin >> n >> m;
        for (int i = 1; i <= n; i++) {
            cin >> x[i] >> y[i];
        }
        for (int i = 1; i <= n; i++) {
            cin >> o[i];
        }
        ans = 0LL;
        for (int i = 1; i <= n - 2; i++) {
            for (int j = i + 1; j <= n - 1; j++) {
                for (int k = j + 1; k <= n; k++) {
                    if (temp[med(dis(i, j), dis(i, k), dis(j, k))]) {
                        ans += o[i] + o[j] + o[k];
                    }
                }
            }
        }
        cout << ans;
        if (T) cout << '\n';
    }
    cout << endl;
    return 0;
}